﻿using System;
using System.Collections.Generic;
using System.Linq;
using Core.Contract.DataContract;
using Core.DAO;
using Data.BO;
using www.Models;

namespace Controllerss
{
    public class TypeController
    {
        #region value
        private readonly TypeRefBO _typeBo = new TypeRefBO();
        private readonly TypeValueBO _typeValueBo=new TypeValueBO();
        #endregion

        #region ham lay du lieu

        public IQueryable<RelaxTypeDataContract> GeTypeDataContracts()
        {
            using (var scope=new DAOScope())
            {
                var lReturn = _typeBo.GeTypeDataContracts();
                scope.Complete();
                return lReturn;
            }
        }
        #endregion

        #region Add

        public long Add(List<RelaxTypeDataContract> typeDataContract)
        {
            using (var scope=new DAOScope())
            {
                var iD = 0;
                var type = new TYPE_REF
                {
                    ord = typeDataContract[0].Ord,
                    active = typeDataContract[0].Active
                };
                _typeBo.Add(type);

                var first = _typeBo.GetTypeDataContract().OrderByDescending(o => o.Id).FirstOrDefault();
                if (first != null)
                {
                    iD = Convert.ToInt32(first.Id);
                    foreach (var item in typeDataContract)
                    {
                        var typeValue = new TYPE_VALUE
                        {
                            typeid = iD,
                            languagecode = item.Languagecode,
                            name = item.Name,
                            tag = item.Tag,
                            decription = item.Decription
                        };
                        _typeValueBo.Add(typeValue);
                    }
                }
                   
                scope.Complete();
                return iD;
            }
        }
        #endregion

        #region Update
        public Boolean Update(List<RelaxTypeDataContract> typeDataContract)
        {
            using (var scope = new DAOScope())
            {
                var type = new TYPE_REF
                {
                    id = Convert.ToInt32(typeDataContract[0].Id),
                    active = typeDataContract[0].Active,
                    ord = typeDataContract[0].Ord
                };
                _typeBo.Update(type);
                foreach (var item in typeDataContract)
                {
                    var typeValue = new TYPE_VALUE
                    {
                        name = item.Name,
                        tag = item.Tag,
                        languagecode = item.Languagecode,
                        typeid = Convert.ToInt32(item.Id),
                        decription = item.Decription
                    };
                    if (!_typeValueBo.Update(typeValue))
                    {
                        _typeValueBo.Add(typeValue);
                    }
                }
                scope.Complete();
                return true;
            }
        }
        #endregion

        #region Delete

        public Boolean Delete(List<long> lId, long iLanguageCode)
        {
            using (var scope=new DAOScope())
            {
                foreach (var id in lId)
                {
                    _typeValueBo.Delete(id,iLanguageCode);
                }
                scope.Complete();
                return true;
            }
        }
        #endregion
    }
}
